/**
 * @Author @fiber-cloud
 * @Date 2025/9/4 20:35
 * 单例模式——懒汉模式
 */

//单线程版本
class Singleton1{
    private static Singleton1 singleton1 = null;

    private Singleton1(){};

    public static Singleton1 getSingleton1(){
        if (singleton1 == null){
            singleton1 = new Singleton1();
        }
        return singleton1;
    }
}

//多线程版本
class Singleton2{
    private static Singleton2 singleton = null;

    private Singleton2(){};

    public synchronized static Singleton2 getSingleton(){
        if (singleton == null){
            singleton = new Singleton2();
        }
        return singleton;
    }
}

public class ThreadDemo11 {
    public static void main(String[] args) {
        Singleton1 s1 = Singleton1.getSingleton1();
        Singleton1 s2 = Singleton1.getSingleton1();

        //这里再new一个会报错
         //Singleton1 s3 = new Singleton1();
        System.out.println(s1 == s2);


        Singleton2 s3 = Singleton2.getSingleton();
        Singleton2 s4 = Singleton2.getSingleton();

        //这里再new一个会报错
        //Singleton1 s3 = new Singleton1();
        System.out.println(s3 == s4);

    }
}
